using RayDen.Library.Core;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Engines.MCMC.PSS
{
    public class PsSample : Sample
    {
        public float[] PrevSamples;
        public int MaxDimPrev, CurrentDim, MutationsCount;
        public bool LargeStep;
        private PsMltSampler sampler;

        public PsSample(ISampler sampler) : base(sampler)
        {
            this.sampler = (PsMltSampler)sampler;
            sampleData = new float[this.sampler.MaxSampleDimension];
            PrevSamples = new float[this.sampler.MaxSampleDimension];
            MaxDimPrev = 0;
        }

        public PsSample(Sample sample) : base(sample)
        {
        }

        public new float GetLazyValue()
        {
            if (CurrentDim == 0)
            {
                LargeStep = sampler.NextFloat() < sampler.LargeStepProbability;
                if (LargeStep)
                    MutationsCount = 0;
            }
            if (CurrentDim == sampler.MaxSampleDimension)
            {
                return sampler.NextFloat();
            }
            if (LargeStep || CurrentDim >= MaxDimPrev)
            {
                sampleData[CurrentDim] = sampler.NextFloat();
            }
            else
            {
                sampleData[CurrentDim] = MC.Mutate(sampler.NextFloat(), sampler.NextFloat(), PrevSamples[CurrentDim]);
            }
            return sampleData[CurrentDim++];
        }
    }
}